package com.ruoyi.information.controller.pc;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.information.domain.pc.Recom;
import com.ruoyi.information.service.pc.IRecomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 推荐列表Controller
 *
 * @author ruoyi
 * @date 2023-05-25
 */
@RestController
@RequestMapping("/recom")
public class RecomController extends BaseController {
    @Autowired
    private IRecomService recomService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询推荐列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Recom recom) {
        startPage();
        List<Recom> list = recomService.selectRecomList(recom);
        return getDataTable(list);
    }

    @GetMapping("/addCount")
    public AjaxResult addCount(@RequestParam("type") Integer type) {

        String s;

        switch (type) {
            case 1: {
                s = "index1";
                redisSet(s);
                break;
            }
            case 2: {
                s = "index2";
                redisSet(s);
                break;
            }

            case 3: {
                s = "index3";
                redisSet(s);
                break;
            }
            case 4: {
                s = "index4";
                redisSet(s);
                break;
            }
            case 5: {
                s = "index5";
                redisSet(s);
                break;
            }
            case 6: {
                s = "index6";
                redisSet(s);
                break;
            }
            case 7: {
                s = "index7";
                redisSet(s);
                break;
            }
            case 8: {
                s = "index8";
                redisSet(s);
                break;
            }
        }
        return success();
    }

    @GetMapping("/lookCount")
    public AjaxResult lookCount() {
        redisService.setCacheObject("XLook:" + UUID.fastUUID(), 1, 30L, TimeUnit.MINUTES);
        return success();
    }

    public void redisSet(String s) {
        /*全*/
        Integer size = redisService.getCacheObject(s);
        if (size == null) {
            redisService.setCacheObject(s, 1);
        } else {
            redisService.setCacheObject(s, size + 1);
        }
        /*周*/
        Long timeout = DateUtils.parseDate(DateUtils.getWeekEnd()).getTime() - DateUtils.getNowDate().getTime();
        size = redisService.getCacheObject(s + "week");
        if (size == null) {
            redisService.setCacheObject(s + "week", 1, timeout, TimeUnit.MILLISECONDS);
        } else {
            redisService.setCacheObject(s + "week", size + 1, timeout, TimeUnit.MILLISECONDS);
        }
        /*月*/
        timeout = DateUtils.parseDate(DateUtils.getMonthEnd()).getTime() - DateUtils.getNowDate().getTime();
        size = redisService.getCacheObject(s + "month");
        if (size == null) {
            redisService.setCacheObject(s + "month", 1, timeout, TimeUnit.MILLISECONDS);
        } else {
            redisService.setCacheObject(s + "month", size + 1, timeout, TimeUnit.MILLISECONDS);
        }
    }
}
